local UserDataHelper = {}

function UserDataHelper.getNumByTypeValue(type, value)
    local TypeHelper = require("utils.TypeHelper")
    local typeName = TypeHelper.getTypeName(type)
    local getFunc = UserDataHelper["get_size_" .. typeName]
    if getFunc then
        return getFunc(value)
    end
    return 0
end

function UserDataHelper.get_size_items(id)
    local num = G_UserData:getItems():getItemNum(id)
    return num
end

function UserDataHelper.get_size_resource(id)
    local num = G_UserData:getResource():getResNum(id)
    return num
end

function UserDataHelper.useSomeThing(type, value, size)
    local TypeHelper = require("utils.TypeHelper")
    local typeName = TypeHelper.getTypeName(type)
    local useFunc = UserDataHelper["use_" .. typeName]
    if useFunc then
        return useFunc(value, size)
    end
    return false
end

function UserDataHelper.use_item(value, size)
    return G_UserData:getItems():useItem(value, size)
end

function UserDataHelper.use_resource(value, size)
    return G_UserData:getResource():useRes(value, size)
end

function UserDataHelper.shopBuy(shopCfg)
    local haveNum = UserDataHelper.getNumByTypeValue(shopCfg.price_type, shopCfg.price_value)
    if haveNum >= shopCfg.price_size then
        if UserDataHelper.useSomeThing(shopCfg.price_type, shopCfg.price_value, shopCfg.price_size) then
            local award = {}
            award.type = shopCfg.item_type
            award.value = shopCfg.item_value
            award.size = shopCfg.item_size
            G_DropManager:drop(award)
            return true
        else
            return false
        end
    else
        return false
    end
end

return UserDataHelper
